
package com.epam.android.training.day7.task1;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;

/**
 * Processed service payload data and shows message in notification area.
 * 
 * @author Anton Novikau
 */
public class ServiceInteractionReciever extends BroadcastReceiver {

  private static final int MESSAGE_ID = 0;

  /**
   * This method is called when the BroadcastReceiver is receiving an Intent
   * broadcast.
   */
  @Override
  public void onReceive(Context context, Intent intent) {
    Resources resources = context.getResources();

    // get payload from intent
    String payloadKey = resources.getString(R.string.random_number_bundle_key);
    int payload = intent.getIntExtra(payloadKey, 0);

    // create notification message
    NotificationManager notificationManager = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);

    String message = resources.getString(R.string.notification_message, payload);
    String ticker = resources.getString(R.string.notification_ticker);
    Notification notification = new Notification(android.R.drawable.ic_menu_add, ticker,
        System.currentTimeMillis());
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context,
        MainActivity.class), 0);
    notification.contentIntent = contentIntent;
    // Set the info for the views that show in the notification panel.
    notification.setLatestEventInfo(context, ticker, message, contentIntent);

    // sends message to notification area
    notificationManager.notify(MESSAGE_ID, notification);
  }
}
